$(function() {
    //点击获取 tabs 列表不同的信息
    jQuery(".slideTxtBox").slide({
        trigger:"click",
        delayTime:700,
        startFun:function( i,c,s )
        {  
           if(i==0){ //个人简介

           }else if(i==1){ //文章分享
        	   
           }else if(i==2){ //咨询回复
                GetLawyerQuestions();
           }else if(i==3){ //用户评价
                GetLawyerEvaluations();
           }else if(i==4){ //成交记录

           }else{
            
          }
        }

    });
  });

  //点击关注
  $(document).on('click', '.attention', function(e) {
    if(islogin == "" || islogin == null){
      layer.msg("请登录后在关注！");
      return false;
    }else{
      var follow_id = $(this).attr("data-id");
      var token = $("input:[name='_token']").val();
      var params = {};
      params['follow_id'] = follow_id;
      params['user_id'] = user_id;
      params['_token'] = token;
      var url = jumpUrl+'/follow';
      $.post(url,params,function(data){

        }, "json").error(function(data) {
          var error =  $.parseJSON(data.responseText);
          $.each(error.error_messages, function(i, item){
            layer.msg(item[0]);
          });
        }).success(function(data) {
          var obj = data.data;
          layer.msg(obj);
          $('.follow_text').text("取消关注");
          $('.attention').attr("class","unfollow");
          $('.zanicon-off').attr("class","zanicon");
        })
    }
    stopDefault(e);
  });
    //取消关注
  $(document).on('click', '.unfollow', function(e) {
    if(islogin == "" || islogin == null){
      layer.msg("请登录后在关注！");
      return false;
    }else{
      var $this = $(this);
      var follow_id = $(this).attr("data-id");
      var token = $("input:[name='_token']").val();
      var params = {};
      params['follow_id'] = follow_id;
      params['user_id'] = user_id;
      params['_token'] = token;
      var url = jumpUrl+'/follow/remove';
      $.post(url,params,function(data){
        
        }, "json").error(function(data) {
          var error =  $.parseJSON(data.responseText);
          $.each(error.error_messages, function(i, item){
            layer.msg(item[0]);
          });

        }).success(function(data) {
          var obj = data.data;
          layer.msg(obj);
          $('.follow_text').text("关注");
          $('.unfollow').attr("class","attention");
          $('.zanicon').attr("class","zanicon-off");
        })
      }
      stopDefault(e);
    });

//咨询回复的方法
function GetLawyerQuestions(){
    $(".comments-list ul").html('');
    var loading = '<li><div class="loading">加载中...</div></li>'; 
    $("#page").append(loading);
    var url = jumpUrl+'/lawyer/'+id+'/questions';
    var str ="";
    $.get(url,function(data){
          
        }, "json").error(function(data) {
            layer.msg("请求失败！")
            
        }).success(function(data) {
          $(".loading").parent().remove();
          var obj = data.data;
          if(obj != null && obj.length != 0) {
             for(var i=0; i<obj.length; i++){

                str+='<li class="comments-item bor3">'
                    str+='<div class="comments-item-bd">'
                        str+='<div class="ui_avatar">'
                            str+='<img src="'+obj[i].creator_avatar+'" width="100" height="100" style="border-radius: 50%"/>'
                        str+='</div>'
                        str+='<div class="comments-content">'
                            
                            str+='<a class="nickname" href="'+jumpUrl+'/question/'+obj[i].id+'" target="_self"><span class="title">'+obj[i].title+'</span></a>'
                            str+='<p>'
                              str+='<span class="nickname">普通用户</span>'
                              str+='<span class="c-tx3">'+obj[i].created_at+'</span>'
                            str+='<p>'
                            if(obj[i].content.length > 90) {
                              str+='<p class="textcontent">'+obj[i].content.substring(0,90)+' . . .</p>'
                            }else{
                              str+='<p class="textcontent">'+obj[i].content+'</p>'
                            }
                            str+='<div><a href="'+jumpUrl+'/question/'+obj[i].id+'" target="_self">查看详情</a></div>'
                        str+='</div>'
                    str+='</div>'
                str+='</li>';
              } 
          }else{
            str="";
            str+='<p class="nodata">';
              str+='暂无数据！';
            str+='</p>';
          }
          $(".comments-list ul").html(str);
          $(".comments-list ul li").first().css("border-bottom","1px solid #ccc");
          $(".comments-list ul li").last().css("border-bottom","none");
       });
    }

// // 用户评论
function GetLawyerEvaluations() {
    $(".evaluations-list ul").html('');
    var loading = '<li><div class="loading">加载中...</div></li>'; 
    $("#page1").append(loading);
    var str = "";
    $.get(single_lawyer_evaluation_url, function(data){
    }, "json").error(function(data) {
      var status_code = $.parseJSON(data.responseText);
      layer.msg(status_code.message);
    }).success(function(data) {
      $(".loading").parent().remove();
      var questions = data.data;
      if(questions != null && questions.length != 0) {
          $.each(questions, function(key, item) {

              str +='<dl class="artivleSharelist clear">'
                str +='<dt><img class="userImg" src="' + item.creator_avatar + '" width="100" height="100" style="border-radius:50%" alt=""></dt>'
                str +='<dd class="title">'
                str +='<a>'+item.creator_realname+'&nbsp;&nbsp;&nbsp;&nbsp;</a>'
                str +='<a href="javascript:void(0)">'
                  for (var i=0; i<item.evaluate_score; i++) {
                  str +='<img src="'+jumpUrl+'/img/star-on.png">'
                  }
                  for (var i=0; i<5-item.evaluate_score; i++) {
                  str +='<img src="'+jumpUrl+'/img/star-off.png">'
                  }
                str +='</a>'
                str +='</dd>'
                if (item.evaluate_comment.length >90) {
                str +='<dd class="issuetxt"><p>'+item.evaluate_comment.substring(0,90)+'...</p></dd>'
              }else{
                str +='<dd class="issuetxt"><p>'+item.evaluate_comment+'</p></dd>'
              };
                str +='<dd>'
                  str +='<a class="evaluatelabel">服务态度好</a>'
                  str +='<a class="evaluatelabel">办事效率高</a>'
                  str +='<a class="evaluatelabel">响应速度快</a>'
                  str +='<a class="evaluatelabel">专业程度高</a>'
                  str +='<a class="evaluatelabel">服务周到</a>'
                  str +='<label class="dateTime">'+item.evaluate_created_at+'</label>'
                str +='</dd>'
              str +='</dl>';
          });
          
      } else {
          str="";
          str+='<li class="nodata">';
            str+='暂无数据！';
          str+='</li>';
      }
      $(".evaluations-list ul").html(str);
      $(".evaluations-list ul dl").last().css("border-bottom","none");
    });
}



$(function() {

  // 付费咨询 弹出框
      $(".folder .advisory").click( function (e) {
          var Popup02 = $("#consulting");
          layer.open({
            type: 1, //基本层类型  可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）。 若你采用layer.open({type: 1})方式调用，则type为必填项（信息框除外）
            title: false, //标题
            closeBtn: true,  //关闭按钮
            shade:0.3,  //遮罩默认是0.3透明度
            area: ['477px', '515px'],
            skin: 'yourclass', //样式类名
            content: Popup02 //内容
        });  
        if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){
          $(".layui-layer").css('top','100px');
        } 
      }); 
//提交咨询
      $(".quesissSbmint .subAdvisory").click( function (e) { 
          var title = $.trim($("#title").val());
          var content = $.trim($("#content").val());
          var work_scope = $.trim($("#work_scope").val());
          var type = $(".caseType input:checked").attr('value');
          var WeiXinImg = '<div class="weixincode"><br><br>'
                  WeiXinImg+='<p>提交成功！感谢您的咨询，律团网的律师会尽快为您解答！</p><br>'
                  WeiXinImg+='<p class="WeiXinImg"></p>'
                  WeiXinImg+='<br><p>扫一扫<span style="color:#ff446f">关注服务号，随时随地收到回复</span>'
                WeiXinImg+='</div>'
          if(work_scope.length < 1){
              layer.msg('请选择案件类型！', {time: 2000});
              return false;
          }else if(title.length<1){
              layer.msg('标题不能为空！', {time: 2000});
              return false;
          }else if(title.length>15){
              layer.msg('标题不能超过15字！', {time: 2000});
              return false;
          }else if(content.length < 1){
              layer.msg('内容不能为空！', {time: 2000});
              return false;
          }else if(content.length < 20){
              layer.msg('内容字数不能小于20字！', {time: 2000});
              return false;
          }else{var loading = layer.msg('加载中...', {icon: 16});
          var inquery = $("#inquery_create_form");
          $(this).attr("disabled","disabled");
          var url = jumpUrl+'/center/question/create/'+type;
          $.post(url, inquery.serialize(),
             function(data){
                 console.info(data);
                $(':input',inquery).not(':button, :submit, :reset, :hidden').val(''); 
                if(islogin == "" || islogin == null){
                    var weixincode = layer.open({
                          type: 1, //基本层类型  可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）。 若你采用layer.open({type: 1})方式调用，则type为必填项（信息框除外）
                          title: false, //标题
                          closeBtn: true,  //关闭按钮
                          shade:0.3,  //遮罩默认是0.3透明度
                          shadeClose: false,  //是否点击遮罩关闭
                          area: ['450px', '400px'],
                          skin: 'yourclass', //样式类名
                          content: layerWeiXinCode //内容
                    });
                }else{
                    layer.closeAll();
                    var weixincode = layer.open({
                          type: 1, //基本层类型  可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）。 若你采用layer.open({type: 1})方式调用，则type为必填项（信息框除外）
                          title: false, //标题
                          closeBtn: true,  //关闭按钮
                          shade:0.3,  //遮罩默认是0.3透明度
                          shadeClose: false,  //是否点击遮罩关闭
                          area: ['450px', '360px'],
                          skin: 'yourclass', //样式类名
                          content: WeiXinImg //内容
                    });
                    
                }
                layer.close(loading);
             }, "json").error(function(data) {
               var obj = $.parseJSON(data.responseText);
               /*if(obj.error_messages.title.length > 0){
                alert(obj.error_messages.title);
               }
               if(obj.error_messages.content.length > 0){
                alert(obj.error_messages.content);
               }*/
             }).success(function(data){
              var obj = data.data;
              var id = obj.id;
             });
             
             return true;
      }
      stopDefault(e);
        })

       //获取擅长领域的数据
          $.get(user_lawyer_url, function(data){
              }, "json").error(function(data) {
                var status_code = $.parseJSON(data.responseText);
                layer.msg(status_code.message);
              }).success(function(data) {
                 var obj = data.data;
                  var work_scope = obj;
                  var options = '';
                  $.each(work_scope, function(key, item) {
                      options+='<option value="'+item.key+'">'+item.value+'</option>';
                  });
                  $("#work_scope").append(options);
            });
      //文章分享
      $(document).on( "click", ".fx", function(){

        $(".lvartivleShare dl").remove();
        var loading = '<li><div class="loading">加载中...</div></li>'; 
        $(".lvartivleShare").append(loading);
        var url = jumpUrl+'/lawyer/'+id+'/articles';
          $.get(url, function(data){
              }, "json").error(function(data) {
                
                var status_code = $.parseJSON(data.responseText);
                layer.msg(status_code.message);
              }).success(function(data) {
                $(".loading").parent().remove();
                var str = "";
                var obj = data.data;
                // console.info(obj)
                if(obj != null && obj.length != 0){
                   for (var i = 0; i < obj.length; i++) {

                    str +='<dl class="artivleSharelist clear">'
                      str +='<dt><img class="userImg" src="'+obj[i].img+'" width="200" height="140" alt="" /></dt>'
                      str +='<dd class="title"><b><a href="">'+obj[i].title+'</a></b></dd>'
                      if (obj[i].info.length > 130) {
                         str +='<dd class="issuetxt">'+obj[i].info.substring(0,130)+'</dd>'
                      }else{
                         str +='<dd class="issuetxt">'+obj[i].info+'</dd>'
                      };
                     
                      str +='<dd>'
                          str +='<label class="dateTime">'+obj[i].created_at+'</label>'
                          str +='<div class="numlr">'
                              str +='<a href="javascript:void(0)" title="阅读"><label class="readNumber"><i class="icon-read"></i>'+obj[i].hits+'</label></a>'
                              str +='<a href="javascript:void(0)" title="点赞"><label class="like"><i class="icon-like"></i>'+obj[i].likes+'</label></a>'
                              str +='<a href="javascript:void(0)" title="评论"><label class="review last"><i class="icon-comment"></i>'+obj[i].comments+'</label></a>'
                          str +='</div>'
                      str +='</dd>'
                  str +='</dl>'
                  }
                }else {
                      str+='<dl class="nodata">';
                        str+='暂无数据！';
                      str+='</dl>';
                  }
                $(".lvartivleShare").append(str);
                $(".lvartivleShare dl").last().css("border-bottom","none");
            });
        });

      //成交记录
      $(document).on( "click", ".jl", function(){
        $(".lvtrading li").remove();
        var loading = '<li><div class="loading">加载中...</div></li>'; 
        $(".lvtrading").append(loading);
        var lawyer_view_url = jumpUrl+'/lawyer/'+lawyer_id+'/deals';
          $.get(lawyer_view_url, function(data){
              }, "json").error(function(data) {
                var status_code = $.parseJSON(data.responseText);
                layer.msg(status_code.message);
              }).success(function(data) {
                $(".loading").parent().remove();
                var record = "";
                var obj = data.data;
                if(obj != null && obj.length != 0){
                   for (var i = 0; i < obj.length; i++) {
                    if (!obj[i].creator_name){obj[i].creator_name = "普通用户"}
                    if (!obj[i].qty){obj[i].qty = 1}
                    record+='<li class="tradingRecord">'
                      record+='<span class="span">'+obj[i].creator_name+'</span>'
                      record+='<span class="span">'+obj[i].sum+'</span>'
                      record+='<span class="span">'+obj[i].qty+'</span>'
                      record+='<span class="span">'+obj[i].updated_at+'</span>'
                      record+='<span class="span">'+obj[i].description+'</span>'
                    record+='</li>';
                  }
                }else {
                      record+='<li class="nodata">';
                        record+='暂无数据！';
                      record+='</li>';
                  }
                $(".lvtrading").append(record);
                for(var j=0; j<obj.length; j++){
                  if (j%2 == 1) {
                  $(".lvtrading .tradingRecord").eq(j).css("background-color","#efefef");
                    }
                }
            });

        });
        
        
        

      //上传图片的插件
      //初始化jQuery的文件上传控件
      $('#avatar_file').fileupload({
          limitMultiFileUploads : 1,
          limitMultiFileUploadSize : 1,
          limitMultiFileUploadSizeOverhead : 512,
          limitConcurrentUploads : 1,
            sequentialUploads : true,
          headers: 
            {"Accept":"application/json"}, //必须有
              dataType: "json", //表示返回值类型，不必须
          formData:{
            _token:token
          },
          messages: {uploadedBytes: 'Uploaded bytes exceed file size'},
          done: function (e, data) {//设置文件上传完毕事件的回调函数 
              var obj = data.result;
              var avatar = obj.data.file_path;
              var file_name = obj.data.file_name;
              var fileExt=( /[^.]+$/.exec(avatar.toUpperCase())); 
              var type = fileExt[0];
              $(".btn-success").append('<input type="hidden" name="file_paths[]" value="'+avatar+'"><input type="hidden" name="file_names[]" value="'+file_name+'">');
        	  	layer.msg("上传文件成功！");
            	  $(".filename ul").append('<li>'+file_name+'&nbsp;&nbsp;<a class="delet" href="javascript:void(0);" style="color:#004799;" title="删除">删除</a></li>');//文件名
            	  $(".avatar_js_cover").show();
            	   var fileLen = $(".filename ul li").length;
            	   var nameLen = $("input:hidden[name='file_names[]']").length;
            	   if(fileLen>=2){
            			   $('#avatar_file').attr("disabled",true);
            	   }
              
              
            },
            fail: function(e, data) {
                //错误提示
                /*console.info(data)*/
            }
        });

      //删除上传的文件
      $(document).on( "click", ".delet", function(e) {
    	 $this = $(this).parent();
    	 $this.hide();
       index = $this.index();
       $(this).parent().remove();
       $("input:hidden[name='file_paths[]']").eq(index).remove();
       $("input:hidden[name='file_names[]']").eq(index).remove();
       var fileLen = $(".filename ul li").length;
       var nameLen = $("input:hidden[name='file_names[]']").length;
       if(fileLen<2){
    	   $('#avatar_file').attr("disabled",false);
       }
        stopDefault(e);
      })


  });
